﻿<template>
  <div class="kaoshi-container">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
      <el-form :model="queryParams" ref="queryForm" labelWidth="90">
        <el-row>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item label="关键字">
              <el-input v-model="queryParams.searchKey" clearable="" placeholder="请输入模糊查询关键字"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="考试名称">
              <el-input v-model="queryParams.name" clearable="" placeholder="请输入考试名称"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="学年学期">
              <el-select clearable="" filterable="" v-model="queryParams.xuenianXueqiId" placeholder="请选择学年学期">
                <el-option v-for="(item,index) in  xuenianXueqiXuenianXueqiIdDropdownList" :key="index" :value="item.value" :label="item.label" />
                
              </el-select>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="考试类别">
              <el-select clearable="" v-model="queryParams.leiBie" placeholder="请选择考试类别">
                <el-option v-for="(item,index) in dl('kaoshi_leibie')" :key="index" :value="item.code" :label="`[${item.code}] ${item.value}`" />
                
              </el-select>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="考号前缀">
              <el-input v-model="queryParams.perNum" clearable="" placeholder="请输入考号前缀"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="开始时间">
              <el-date-picker placeholder="请选择开始时间" value-format="YYYY/MM/DD" type="daterange" v-model="queryParams.startDateRange" />
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="结束时间">
              <el-date-picker placeholder="请选择结束时间" value-format="YYYY/MM/DD" type="daterange" v-model="queryParams.endDateRange" />
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="考试状态">
              <el-select clearable="" v-model="queryParams.status" placeholder="请选择考试状态">
                <el-option v-for="(item,index) in dl('kaoshi_status')" :key="index" :value="item.code" :label="`[${item.code}] ${item.value}`" />
                
              </el-select>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="备注">
              <el-input v-model="queryParams.beizhu" clearable="" placeholder="请输入备注"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="6" class="mb10">
            <el-form-item>
              <el-button-group>
                <el-button type="primary"  icon="ele-Search" @click="handleQuery" auth="'kaoshi:page'"> 查询 </el-button>
                <el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>
                <el-button icon="ele-ZoomIn" @click="changeAdvanceQueryUI" v-if="!showAdvanceQueryUI"> 高级 </el-button>
                <el-button icon="ele-ZoomOut" @click="changeAdvanceQueryUI" v-if="showAdvanceQueryUI"> 隐藏 </el-button>
                
              </el-button-group>
              
              <el-button-group style="margin-left:20px">
                <el-button type="primary" icon="ele-Plus" @click="openAddKaoshi" auth="'kaoshi:add'"> 新增 </el-button>
                
              </el-button-group>
              
            </el-form-item>
            
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="full-table" shadow="hover" style="margin-top: 8px">
      <el-table
				:data="tableData"
				style="width: 100%"
				loading="loading"
				tooltip-effect="light"
				row-key="id"
				border="">
        <el-table-column type="index" label="序号" width="55" align="center"/>
        <el-table-column prop="name" label="考试名称" width="240" show-overflow-tooltip="" />
        <el-table-column prop="xuenianXueqiId" label="学年学期" width="120" show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.xuenianXueqiIdFullname}}</span>
          </template>
        </el-table-column>
          <el-table-column prop="leiBie" label="考试类别" width="100" show-overflow-tooltip="" >
            <template #default="scope">
              <el-tag :type="di('kaoshi_leibie', scope.row.leiBie)?.tagType"> {{di("kaoshi_leibie", scope.row.leiBie)?.value}} </el-tag>
            </template>
          </el-table-column>
        <el-table-column prop="perNum" label="考号前缀" width="80" show-overflow-tooltip="" />
        <el-table-column prop="startDate" label="开始时间" width="140" show-overflow-tooltip="" />
        <el-table-column prop="endDate" label="结束时间" width="140" show-overflow-tooltip="" />
          <el-table-column prop="status" label="考试状态" width="60" show-overflow-tooltip="" >
            <template #default="scope">
              <el-tag :type="di('kaoshi_status', scope.row.status)?.tagType"> {{di("kaoshi_status", scope.row.status)?.value}} </el-tag>
            </template>
          </el-table-column>
        <el-table-column prop="beizhu" label="备注" width="140" show-overflow-tooltip="" />
        <el-table-column label="操作" width="550" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('kaoshi:edit') || auth('kaoshi:delete')">
          <template #default="scope">
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openEditKaoshi(scope.row)" auth="'kaoshi:edit'"> 编辑 </el-button>
            <el-button icon="ele-Delete" size="small" text="" type="primary" @click="delKaoshi(scope.row)" auth="'kaoshi:delete'"> 删除 </el-button>
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="copyKaoshiHandle(scope.row)" auth="'kaoshi:copy'"> 复制 </el-button>
            
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openKaoshiXueke(scope.row)" auth="'kaoshiXueke:page'"> 学科 </el-button>
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openShichang(scope.row)" auth="'shichang:page'"> 试场 </el-button>
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openKaosheng(scope.row)" auth="'kaosheng:page'"> 考生 </el-button>
            <el-button icon="ele-Printer" size="small" text="" type="primary" @click="openPrintBanji(scope.row)" auth="'kaoshi:printBanji'"> 班级座位表 </el-button>
            <el-button icon="ele-Printer" size="small" text="" type="primary" @click="openPrintShichang(scope.row)" auth="'kaoshi:printShichang'"> 验收单 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
				v-model:currentPage="tableParams.page"
				v-model:page-size="tableParams.pageSize"
				:total="tableParams.total"
				:page-sizes="[10, 20, 50, 100, 200, 500]"
				small=""
				background=""
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
	/>
      <editDialog
        ref="editDialogRef"
        :title="editKaoshiTitle"
        @reloadTable="handleQuery"
      />
      <kaoshiXuekeDialog
        ref="kaoshiXuekeDialogRef"
        :title="kaoshiXuekeTitle"
        @reloadTable="handleQuery"
      />
      <shichangDialog
        ref="shichangDialogRef"
        :title="kaoshiXuekeTitle"
        @reloadTable="handleQuery"
      />
      <kaoshengDialog
        ref="kaoshengDialogRef"
        :title="kaoshiXuekeTitle"
        @reloadTable="handleQuery"
      />
      <printDialog
        ref="printDialogRef"
        :title="kaoshiXuekeTitle"
        @reloadTable="handleQuery" />
    </el-card>
  </div>
</template>

<script lang="ts" setup="" name="kaoshi">
  import { ref } from "vue";
  import { ElMessageBox, ElMessage } from "element-plus";
  import { auth } from '/@/utils/authFunction';
  import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
  import { formatDate } from '/@/utils/formatTime';

  // 推荐设置操作 width 为 200
  import { hiprint } from 'vue-plugin-hiprint';
  import { SysPrintApi } from '/@/api-services/api';
  import { SysPrint } from '/@/api-services/models';
  import printDialog from '/@/views/system/print/component/hiprint/preview.vue'

  import editDialog from '/@/views/main/kaoshi/component/editDialog.vue'
  import { pageKaoshi, deleteKaoshi, printBanji,printJiaoxueBan,printShichang,copyKaoshi } from '/@/api/main/kaoshi';
  import { getXuenianXueqiXuenianXueqiIdDropdown } from '/@/api/main/kaoshi';
  import kaoshiXuekeDialog from '/@/views/main/kaoshiXueke/index.vue';
  import shichangDialog from '/@/views/main/shichang/index.vue';
  import kaoshengDialog from '/@/views/main/kaosheng/index.vue';

  import { getAPI } from '/@/utils/axios-utils';

  import { SysFileApi } from '/@/api-services/api';
  import { SysFile } from '/@/api-services/models';

  const showAdvanceQueryUI = ref(false);
  const printDialogRef = ref();
  const editDialogRef = ref();
  const kaoshiXuekeDialogRef = ref();
  const shichangDialogRef = ref();
  const kaoshengDialogRef = ref();
  const loading = ref(false);
  const tableData = ref<any>([]);
  const queryParams = ref<any>({});
  const tableParams = ref({
    page: 1,
    pageSize: 10,
    total: 0,
  });

  const printKaoshiTitle = ref("");
  const editKaoshiTitle = ref("");
  const kaoshiXuekeTitle = ref("");
  const kaoshiId = ref(0);
  

  // 改变高级查询的控件显示状态
  const changeAdvanceQueryUI = () => {
    showAdvanceQueryUI.value = !showAdvanceQueryUI.value;
  }
  

  // 查询操作
  const handleQuery = async () => {
    loading.value = true;
    var res = await pageKaoshi(Object.assign(queryParams.value, tableParams.value));
    tableData.value = res.data.result?.items ?? [];
    tableParams.value.total = res.data.result?.total;
    loading.value = false;
  };

  // 打开新增页面
  const openAddKaoshi = () => {
    editKaoshiTitle.value = '添加考试';
    editDialogRef.value.openDialog({});
  };

  const openKaoshiXueke = async (row: any) => {
    kaoshiXuekeTitle.value = row.name;
    kaoshiXuekeDialogRef.value.openDialog(row);
  }
  const openShichang = async (row: any) => {
    kaoshiXuekeTitle.value = row.name;
    shichangDialogRef.value.openDialog(row);
  }

  const openKaosheng = async (row: any) => {
    kaoshiXuekeTitle.value = row.name;
    kaoshengDialogRef.value.openDialog(row);
  }
  
  // 打开打印页面
  const openPrintKaoshi = async (row: any) => {
    printKaoshiTitle.value = '打印考试';
  }
  
  // 打开编辑页面
  const openEditKaoshi = (row: any) => {
    editKaoshiTitle.value = '编辑考试';
    editDialogRef.value.openDialog(row);
  };

  // 删除
  const delKaoshi = (row: any) => {
    ElMessageBox.confirm(`确定要删除吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
  .then(async () => {
    await deleteKaoshi(row);
    handleQuery();
    ElMessage.success("删除成功");
  })
  .catch(() => {});
  };

  // 改变页面容量
  const handleSizeChange = (val: number) => {
    tableParams.value.pageSize = val;
    handleQuery();
  };

  // 改变页码序号
  const handleCurrentChange = (val: number) => {
    tableParams.value.page = val;
    handleQuery();
  };

  const xuenianXueqiXuenianXueqiIdDropdownList = ref<any>([]); 
  const getXuenianXueqiXuenianXueqiIdDropdownList = async () => {
    let list = await getXuenianXueqiXuenianXueqiIdDropdown();
    xuenianXueqiXuenianXueqiIdDropdownList.value = list.data.result ?? [];
  };

    // 打开打印页面
const openPrintBanji = async (row: any) => {
    kaoshiXuekeTitle.value = row.name;
    var res = await getAPI(SysPrintApi).apiSysPrintPrintNameGet('考试各班学生座位表');
	  var printTemplate = res.data.result as SysPrint;
    var template = JSON.parse(printTemplate.template);
    var resBanji = await printJiaoxueBan({kaoshiId:row.id})
    printDialogRef.value.showDialog(new hiprint.PrintTemplate({template: template}), resBanji.data.result,'考试各班学生座位表',template.panels[0].width);
}

const openPrintShichang = async (row: any) => {
    kaoshiXuekeTitle.value = row.name;
    var res = await getAPI(SysPrintApi).apiSysPrintPrintNameGet('考场验收单');
	  var printTemplate = res.data.result as SysPrint;
    var template = JSON.parse(printTemplate.template);
    var resShichang = await printShichang({kaoshiId:row.id})
    console.log(resShichang.data.result);
    printDialogRef.value.showDialog(new hiprint.PrintTemplate({template: template}), resShichang.data.result,'考场验收单',template.panels[0].width);
}

const copyKaoshiHandle = async (row: any) => {
    await copyKaoshi(row);
    handleQuery();
    ElMessage.success("复制成功");
}

  getXuenianXueqiXuenianXueqiIdDropdownList();
  
  handleQuery();
</script>
<style scoped>
:deep(.el-ipnut),
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>

